package com.gm.wj.controller;

import com.gm.wj.entity.JotterArticle;
import com.gm.wj.result.Result;
import com.gm.wj.result.ResultFactory;
import com.gm.wj.service.JotterArticleService;
import com.gm.wj.util.SysLoging;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
* @Description: 文章笔记本控制层
* @Author: Huang_shengjun
* @time: 2020/8/12 22:48
*/
@Api(tags = "文章管理相关接口")
@RestController
public class ArticlesController {
    @Autowired
    JotterArticleService jotterArticleService;

    // 添加
    @PostMapping("api/admin/content/article")
    @SysLoging("添加文章信息")
    @ApiOperation("添加文章信息的接口")
    public Result saveArticle(@RequestBody @Valid JotterArticle article) {
        jotterArticleService.addOrUpdate(article);
        return ResultFactory.buildSuccessResult("保存成功",null);
    }

    // 列表
    @GetMapping("/api/article/{size}/{page}")
    @ApiOperation("查询文章列表信息的接口")
    @SysLoging("查询文章列表信息")
    public Result listArticles(@PathVariable("size") int size, @PathVariable("page") int page) {
        return ResultFactory.buildSuccessResult("查询成功",jotterArticleService.listAdmin(page - 1, size));
    }

    // 列表
    @GetMapping("/api/articleIndex/{size}/{page}")
    @ApiOperation("查询文章列表信息的接口")
    @SysLoging("查询文章列表信息")
    public Result listArticlesIndex(@PathVariable("size") int size, @PathVariable("page") int page) {
        return ResultFactory.buildSuccessResult("查询成功",jotterArticleService.list(page - 1, size));
    }

    // 根据id查询
    @GetMapping("/api/article/{id}")
    @ApiOperation("根据id查询文章信息的接口")
    @SysLoging("根据id查询文章信息")
    public Result getOneArticle(@PathVariable("id") int id) {
        return ResultFactory.buildSuccessResult("查询成功",jotterArticleService.findById(id));
    }

    // 删除
    @DeleteMapping("/api/admin/content/article/{id}")
    @ApiOperation("删除文章信息的接口")
    @SysLoging("删除文章信息")
    public Result deleteArticle(@PathVariable("id") int id) {
        jotterArticleService.delete(id);
        return ResultFactory.buildSuccessResult("删除成功",null);
    }
}
